package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Account;
import com.sl.au.entity.EXDELoginInfo;
import com.sl.au.entity.Role;
import com.sl.au.entity.User;
import com.sl.au.repository.AccountRepository;
import com.sl.au.repository.EXDELoginInfoRepository;
import com.sl.au.repository.UserRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.ModuleService;

@Service("accountService")
@Transactional
public class AccountServiceImpl extends BaseServiceImpl implements AccountService {

	@Autowired
	AccountRepository accountRepository;
	@Autowired
	UserRepository userRepository;
	
	@Autowired
	ModuleService moduleService;
	
	@Autowired
	EXDELoginInfoRepository exdeLoginRepository;

	@Override
	public List<Account> getPage(int start, int length, String key, String orderName, Direction dir) {
		Page<Account> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if(StringUtils.hasText(key)){
			pages=accountRepository.getByKey(key,pageable);
		}
		else{
			pages = accountRepository.findAll(pageable);
		}
		
		return pages.getContent();
	}

    @Override
    public Account getAccountData(String name) {
        Account account=new Account();
		account =  accountRepository.findByAccountName(name);
		return account;
    }

    public AccountRepository getRepository() {
		return this.accountRepository;
	}

	@Override
	public Account login(Account account) {
		/*Account a = this.accountRepository.findByAccountNameAndPassword(account.getAccountName(),
				account.getPassword());*/
		
		Account a=this.getByUsernameAndPassord(account.getAccountName(),
				account.getPassword());
		if(null==a) return null;
		if (null != a) {
			a.setLastLoginDate(new Date());
			accountRepository.save(a);
		}
		//获取角色权限
		Role role=a.getRole();
		role.setModuleInfos(moduleService.getModulesInfo(role));		
		
		return a;
	}

	@Override
	public Account getByUsernameAndPassord(String userName, String password) {
		if(!StringUtils.hasText(userName) || !StringUtils.hasText(password)) return null;
		Account a=new Account();
		EXDELoginInfo loginInfo=this.exdeLoginRepository.getByUserNameAndPassword(userName, password);
		if(null==loginInfo)  return null;
		String empId=loginInfo.getEmpId();
	
		User user=userRepository.getByEmpId(empId);
		
		if(null==user) return null;
		
		a=this.getRepository().getByUserId(user.getId());
		if(null==a) return null;
		//设置视图账号信息
		a.setLoginInfo(loginInfo);
		a.setAccountName(loginInfo.getUserName());
		a.setPassword(loginInfo.getPassword());		
		
		return a;
	}

	@Override
	public int getCount(String key) {
		if (!StringUtils.hasText(key))
			return accountRepository.findAll().size();
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		return accountRepository.getByKey(key, pageable).getContent().size();
	}

}
